<?php

/**
 * class news
 * not done (paging)
 * */
class ControllerNewsNews extends Controller {

    public function index() {
        $this->language->load('news/news');
        $this->load->model('catalog/news');
        $this->load->model('catalog/category_news');

        //---------------global--------------------
        $this->document->setTitle($this->language->get('text_news'));
        $this->data['heading_title'] = $this->language->get('text_news');

        //----------breakcrum-----------------------
        $this->data['breadcrumbs'][] = array(
            'href' => HTTP_SERVER,
            'text' => $this->language->get('text_home'),
            'separator' => FALSE
        );
        $this->data['breadcrumbs'][] = array(
            'href' => HTTP_SERVER . 'news/',
            'text' => $this->language->get('text_news'),
            'separator' => $this->language->get('text_separator')
        );

        //----------get request---------------------
		$page = 1;
        if (isset($_GET['page'])) {
            $page = $_GET['page'];
        } 
        //----------list category news--------------
        $category = $this->model_catalog_category_news->getCategories();

        //----------list news limit-----------------------
        $news = $this->model_catalog_news->getnewss(($page - 1) * 20, 20);
        foreach ($news as $key => $result) {
            $first_img = '';
            $output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', html_entity_decode($result['description'], ENT_QUOTES, 'UTF-8'), $matches);
            if ($matches [1]) {
                $first_img = $matches [1] [0];
            } else {
                $first_img = "image/no_image.jpg";
            }
            $this->data['newss'][] = array(
                'item_id' => $result['news_id'],
                'title' => $result['title'],
                'date_added' => date('h:iA d/m/Y', strtotime($result['date_added'])),
                'description' => $result['description'],
                'image' => $first_img,
                'href' => HTTP_SERVER . 'news/' . $result['news_category'] . '/' . $result['seo_name'] . '.html'
            );
        }
        //------------pagination-----------------------------------------
        $pagination = new Pagination();
        $pagination->total = $this->model_catalog_news->gettotalnewss();
        $pagination->page = $page;
        $pagination->limit = 20;
        $pagination->text = $this->language->get('text_pagination');
        $pagination->url = HTTP_SERVER . 'news/?page=%s';
        $this->data['pagination'] = $pagination->render();

        //----------load the template---------------
        if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/news/news.tpl')) {
            $this->template = $this->config->get('config_template') . '/template/news/news.tpl';
        } else {
            $this->template = 'default/template/news/news.tpl';
        }

      
        $this->children = array(
            'common/column_left',
            'common/column_right',
            'common/content_top',
            'common/content_bottom',
            'common/footer',
            'common/header'
        );

        $this->response->setOutput($this->render(TRUE), $this->config->get('config_compression'));
    }

}